package com.gxc.dbcore.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.gxc.dbcore.dao.SqlDao;
import com.gxc.dbcore.service.BeanMapService;
import com.gxc.dbcore.util.CompareItem;
import com.gxc.dbcore.util.EntityDef;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;

@Service("beanMapService")
public class BeanMapServiceImpl implements BeanMapService {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map get(SqlDao sqlDao, EntityDef entityDef, Object idValue) {
		Map params = new HashMap();
		params.put("table", entityDef.getSchema());
		params.put("table", entityDef.getTable());
		params.put("id", entityDef.getId());
		params.put("idValue", idValue);
		return sqlDao.selectOne("webcore.entity.get", params);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int create(SqlDao sqlDao, EntityDef entityDef, Map map) {
		Map params = new HashMap();
		params.put("table", entityDef.getSchema());
		params.put("table", entityDef.getTable());
		params.put("columnList", entityDef.getKeyValueList(map));
		return sqlDao.insert("webcore.entity.create", params);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int update(SqlDao sqlDao, EntityDef entityDef, Map map) {
		Map params = new HashMap();
		params.put("table", entityDef.getSchema());
		params.put("table", entityDef.getTable());
		params.put("id", entityDef.getId());
		params.put("idValue", entityDef.getIdValue(map));
		params.put("columnList", entityDef.getKeyValueList(map));
		return sqlDao.update("webcore.entity.update", params);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public int delete(SqlDao sqlDao, EntityDef entityDef, Object idValue) {
		Map params = new HashMap();
		params.put("table", entityDef.getSchema());
		params.put("table", entityDef.getTable());
		params.put("id", entityDef.getId());
		params.put("idValue", idValue);
		return sqlDao.delete("webcore.entity.delete", params);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public int delete(SqlDao sqlDao, EntityDef entityDef, Object[] idValueArray) {
		if (idValueArray.length > 0) {
			Map params = new HashMap();
			params.put("table", entityDef.getSchema());
			params.put("table", entityDef.getTable());
			params.put("id", entityDef.getId());
			params.put("idValueArray", idValueArray);
			return sqlDao.delete("webcore.entity.deleteBatch", params);
		}
		return 0;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Long count(SqlDao sqlDao, EntityDef entityDef,
			List<CompareItem> conditionList) {
		Map queryParams = new HashMap();
		queryParams.put("table", entityDef.getSchema());
		queryParams.put("table", entityDef.getTable());
		queryParams.put("conditionList", conditionList);
		if (conditionList != null) {
			int conditionNum = 0;
			for (CompareItem item : conditionList) {
				if (item.getCompareValue() != null) {
					conditionNum++;
				}
			}
			queryParams.put("conditionNum", conditionNum);
		}
		return sqlDao.selectOne("webcore.entity.count", queryParams);
	}

	/**
	 * 查询
	 * 
	 * @param sqlDao
	 * @param table
	 * @param conditionList
	 * @param fieldSort
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List list(SqlDao sqlDao, EntityDef entityDef,
			List<CompareItem> conditionList, String fieldSort) {
		Map queryParams = new HashMap();
		queryParams.put("table", entityDef.getSchema());
		queryParams.put("table", entityDef.getTable());
		queryParams.put("conditionList", conditionList);
		queryParams.put("fieldSort", fieldSort);
		return sqlDao.selectList("webcore.entity.list", queryParams);
	}

	/**
	 * 查询分页
	 * 
	 * @param sqlDao
	 * @param table
	 * @param conditionList
	 * @param fieldSort
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public PageList listPage(SqlDao sqlDao, EntityDef entityDef,
			List<CompareItem> conditionList, String fieldSort, PageInfo pageInfo) {
		Map queryParams = new HashMap();
		queryParams.put("table", entityDef.getSchema());
		queryParams.put("table", entityDef.getTable());
		queryParams.put("conditionList", conditionList);
		queryParams.put("fieldSort", fieldSort);
		if (conditionList != null) {
			int conditionNum = 0;
			for (CompareItem item : conditionList) {
				if (item.getCompareValue() != null) {
					conditionNum++;
				}
			}
			queryParams.put("conditionNum", conditionNum);
		}
		return sqlDao.selectPageList("webcore.entity.list",
				"webcore.entity.count", queryParams, pageInfo);
	}

}
